package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Role;
import com.neusoft.entity.User;
import com.neusoft.entity.UserQuery;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.RoleService;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("user")
public class UserController {
    //    private Logger logger = LoggerFactory.getLogger(UserController.class);
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @GetMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        System.out.println("查询单条数据" + id);
        User bean = userService.queryById(id);
        System.out.println("bean对象为：" + bean);
        return bean;
    }

    @GetMapping("queryByRoleName/{name}")
    @ResponseBody
    public Object queryByRoleName(@PathVariable String name) {
        System.out.println("查询单条数据" + name);
        Role bean = roleService.queryByName(name);
        System.out.println("bean对象为：" + bean);
        return bean;
    }

    @PostMapping("deleteById/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println("批量删除数据");
        System.out.println(ids[0]);
        userService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public boolean deleteById1(@PathVariable Integer id) {
        System.out.println("删除单条数据");
        if (id == null) {
            return false;
        }
        System.out.println(id);
        return userService.deleteById1(id);
    }

    @PostMapping("queryAll/{page}/{limit}")
    @ResponseBody
    public Object queryAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody UserQuery bean) throws ParseException {
        System.out.println(page);
        System.out.println(limit);
        System.out.println(bean);
        CommonResult<User> result = new CommonResult<>();
        IPage<User> ipage = userService.queryAllByLimit(page, limit, bean);
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        System.out.println("数据查询成功");
        System.out.println(result);
        return result;
    }


    @PutMapping("save")
    @ResponseBody
    public Object save(@RequestBody User bean) {
        System.out.println("修改和添加数据");
        boolean result = false;
        //判断账号是否存在
        //判断是添加还是修改
        if (bean.getId() != null) {
            //修改
            result = userService.update(bean) > 0;
        } else {
            //添加
            bean.setRegDate(new Date());
            result = userService.insert(bean).getId() != null;
        }
        return result;
    }

    //保存记录last_login_data
    @PutMapping("save1")
    @ResponseBody
    public Object save1(@RequestBody User bean) {
        boolean result = false;
        if (bean.getId() != null) {
            //修改
            result = userService.update(bean) > 0;
            System.out.println("更新last_login_data-------------成功" + result);
        }

        return result;
    }

    //根据密码查询数据库中是否有该用户的信息,更新密码

    @PostMapping("changePassword")
    @ResponseBody
    public void changePassword(@RequestParam("account") String account,
                               @RequestParam("oldPassword") String oldPassword,
                               @RequestParam("newPassword") String newPassword, Model model) {
        System.out.println("获取----------" + account);
        System.out.println("获取----------" + oldPassword);
        System.out.println("获取----------" + newPassword);

        User user = userService.findByUserName(account);
        System.out.println("获取----------" + user);

        if (user != null && (oldPassword).equals(user.getPassword())) {
            userService.changePassword(account, newPassword);
            model.addAttribute("msg","用户存在");
            System.out.println("用户存在");
        }else{
            model.addAttribute("msg","用户不存在或旧密码错误");
        }
    }


    //根据邮箱查询用户信息
    @PostMapping("doPost")
    @ResponseBody
    public User doPost(@RequestParam("email") String email) {

        return userService.getUserByEmail(email);

    }

    @RequestMapping("login")
    @ResponseBody
    public User login(@RequestParam("account") String account, @RequestParam("password") String password) {
        return userService.login(account, password);
    }


}
